package com.example.gitpart03;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private TextView tvSkip;
    private Handler handler = new Handler();
    private Runnable runnable = this::startMainActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        tvSkip = findViewById(R.id.tvSkip);
        tvSkip.setOnClickListener(v -> {
            handler.removeCallbacks(runnable);
            startMainActivity();
        });

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 3秒倒计时
        new CountDownTimer(3000, 1000) {
            public void onTick(long millisUntilFinished) {
                tvSkip.setText("跳过 " + millisUntilFinished / 1000 + "s");
            }
            public void onFinish() {
                tvSkip.setText("跳过 0s");
            }
        }.start();

        handler.postDelayed(runnable, 3000);
    }

    private void startMainActivity() {
        startActivity(new Intent(this, LoginActivity.class)); // 应该跳转到主页而非自身
        finish();
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(runnable);
        super.onDestroy();
    }
}
